1

Create a matrix with 8 rows and 8 columns filles with random numbers in a range between 1 and 1000.
You can use the sample() function to create the create the numbers.
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
fancy_matrix <-
  sample(1:1000, 8*8, replace = TRUE) %>% 
  matrix(nrow = 8, ncol = 8)

fancy_matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]  773  315  438  470  556   77  362  103
## [2,]   73  992  896  290  715  366  448  725
## [3,]  389  997  123  673  258  942  963   37
## [4,]  332  378  210  458  637  455  123  203
## [5,]  497  296  597  383  641  766  192  320
## [6,]  998  931  936  572  947  678  172  606
## [7,]  708  801  494  601  543  331  900  630
## [8,]   21  602   77  467  299   83  460  160

2

Now use this matrix to create a raster layer and plot it.
The raster() function can be fed with matrices to create a raster layer.
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
fancy_raster_layer <-
  raster::raster(fancy_matrix)

raster::plot(fancy_raster_layer)

The raster() function can not only be used to create raster data on the fly, which is also not very interesting. Instead, we can use it to import already prepared to data.

3

Import one of the raster .tiff files in the ./data/ folder of the workshop directory (repository). Do you get any warning messages?
Make sure your file paths are set correctly. You can check them with getwd(). Setting is done with setwd().
immigrants_cologne <-
  raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# The warning message is due to the current transition of CRS definition 
# systems. You can ignore it in this course.

4

Import the data on Immigrants, Germans, and Inhabitants. Add up the Immigrants and Germans to a new layer. Then subtract this new layer from the Inhabitants layer to check whether the inhabitants layer is the same as the sum of Immigrants and Germans. Is it?
You can handle raster layers as any simple data table using + and - operators.
# load all layers
immigrants_cologne <-
  raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
germans_cologne <-
  raster::raster("../data/germans_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
inhabitants_cologne <-
  raster::raster("../data/inhabitants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# create sum layer
immigrants_germans_sum <-
  immigrants_cologne + germans_cologne

# create difference layer
difference_layer <-
  inhabitants_cologne - immigrants_germans_sum

difference_layer
## class      : RasterLayer 
## dimensions : 289, 264, 76296  (nrow, ncol, ncell)
## resolution : 100, 100  (x, y)
## extent     : 4094850, 4121250, 3084050, 3112950  (xmin, xmax, ymin, ymax)
## crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
## source     : memory
## names      : layer 
## values     : -2, 0  (min, max)
# get a summary statistic
summary(difference_layer)
##         layer
## Min.       -2
## 1st Qu.     0
## Median      0
## 3rd Qu.     0
## Max.        0
## NA's    62522
# create a table of counts
difference_layer %>% 
  as.data.frame() %>% 
  table()
## .
##    -2    -1     0 
##    11  2052 11711